/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evangelion.core.utils;

import com.evangelion.core.bean.Proveedor;
import com.evangelion.core.daoImpl.GenericDaoImpl;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Kellycasma
 */
@FacesConverter(value = "proveedor")
public class ProveedorConverter implements Converter, Serializable{
    public static List<Proveedor> linkTypesDB;
    GenericDaoImpl gd;
    
    public ProveedorConverter() {
        this.gd = new GenericDaoImpl();
        linkTypesDB = (List)gd.ListAll(new Proveedor());
    }
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String s) {
        if (s.trim().equals("")) {
            return null;
        } else {
            try {
                int number = Integer.parseInt(s);

                for (Proveedor lt : linkTypesDB) {
                    if (lt.getId() == number) {
                        return lt;
                    }
                }
            } catch(NumberFormatException exception) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid converter"));
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object o) {
        if (o == null || o.equals("")) {
            return "";
        } else {
            return String.valueOf(((Proveedor) o).getId());
        }
    }
    
}
